package com.example.biketask.controller;

import com.example.biketask.dao.Opt1Mapper;
import com.example.biketask.pojo.ClickOperation;
import com.example.biketask.pojo.DragOperation;
import com.example.biketask.pojo.ZoomOperation;
import com.example.biketask.pojo.dto.ClickTimes;
import com.example.biketask.service.DragService;
import com.example.biketask.service.TypeOperationService;
import com.example.biketask.service.ZoomService;
import com.example.biketask.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Controller
public class TestMap {

    @Autowired
    TypeOperationService typeOperationService;

    @Autowired
    ZoomService zoomService;

    @Autowired
    DragService dragService;

    @Autowired
    Opt1Mapper opt1Mapper;


    /*
    @GetMapping(value = "/index")
    public ModelAndView method1(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("nymap");
        System.out.println("nymap.html");
        return modelAndView;
    }

    //视图解析器释放需要下面部分
     spring.mvc.view.suffix=.html
     spring.mvc.view.prefix=static/
     */

    @GetMapping(value = "/hello")
    @ResponseBody
    public String method2(){
        return "Hello World";
    }

    @GetMapping(value = "/index")
    String method3(){
        return "static/nymap.html";
    }

    /*  错误示范
    @GetMapping(value = "index2")
    String method4(){
        return "nymap.html";
    }
    正确返回： static/nymap.html

    这样返回不需要：

    spring.mvc.view.suffix=.html
    spring.mvc.view.prefix=static/
    但是需要
    spring.mvc.static-path-pattern=/static/**
     */




    /*
    No mapping for GET /css/page.css
    No mapping for GET /js/jquery-3.5.1.min.js

    上面index前缀的释放htnl都需要再次释放js和css

    前端页面的路径 ./js/jquery-3.5.1.min.js  【错误释放】

    /js/jquery-3.5.1.min.js  【正确释放】
    不能加点，因为这些资源都要交由dispacherservlet处理，而不是本地
     */



    @PostMapping(value = "/save/click")
    @ResponseBody
    protected String method5(@RequestParam int optType,@RequestParam String stationId, @RequestParam String stationName){
        //System.out.println(optType+stationId+stationName);
        ClickOperation clickOperation = new ClickOperation();
        clickOperation.setOptId(optType);
        clickOperation.setStationId(stationId);
        clickOperation.setStationName(stationName);

        typeOperationService.add_click_option(optType, clickOperation);
        return "ok";

    }

    @PostMapping(value = "/save/zoom")
    @ResponseBody
    protected String method6(@RequestParam int optId,@RequestParam double level){
        //System.out.println(optId+"----"+level);
        ZoomOperation zoomOperation = new ZoomOperation();
        zoomOperation.setStart(15.00000000);
        zoomOperation.setEnd(level);
        zoomService.add_zoom_option(optId,zoomOperation);
        return "ok";
    }

    @PostMapping(value = "/save/drag")
    @ResponseBody
    protected String method7(@RequestParam Integer optType,@RequestParam double dragStartLat, @RequestParam double dragStartLon, @RequestParam double dragEndLat, @RequestParam double dragEndLon){
        //System.out.println(optType+"----"+dragStartLat+"---"+dragStartLon+"---"+dragEndLat+"---"+dragEndLon);
        DragOperation dragOperation = new DragOperation();
        dragOperation.setStartLat(dragStartLat);
        dragOperation.setStartLon(dragStartLon);
        dragOperation.setEndLat(dragEndLat);
        dragOperation.setEndLon(dragEndLon);
        //System.out.println(dragOperation);

        dragService.add_drag_option(optType,dragOperation);
        return "ok";
    }


    @CrossOrigin
    @GetMapping(value = "/clickcounts")
    @ResponseBody
    public JsonResult<List<ClickTimes>> method8(){
        List<ClickTimes> clickOperations = opt1Mapper.select_all_times();
        return JsonResult.getSuccessJR(clickOperations);
    }

    /*
    数据实时推送sse数据推送
     */
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

    @GetMapping(value = "/timeclick")
    public SseEmitter subscribeCC(){

        //获取数据库点击量数据
        //List<ClickTimes> clickTimes = null;

        SseEmitter sseEmitter = new SseEmitter(0L);
        executorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    sseEmitter.send(opt1Mapper.select_all_times());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }, 10,5, TimeUnit.SECONDS);  //每个任务10执行时间，每个5秒执行一次

        return sseEmitter;
    }



    @GetMapping(value = "/click")
    public String method9(){
        return "static/cickcount.html";
    }

    @GetMapping(value = "/click1")
    public String method10(){
        return "static/cickcount1.html";
    }

}
